package com.plan.controller.card;

import com.plan.base.config.property.AppProperty;
import com.plan.base.constant.BaseCode;
import com.plan.base.constant.Constant;
import com.plan.base.entity.Result;
import com.plan.controller.card.request.BaseReq;
import com.plan.controller.card.request.CopyRepositoryTaskReq;
import com.plan.controller.card.response.TaskListVO;
import com.plan.controller.user.response.UserVO;
import com.plan.entity.card.MyTask;
import com.plan.enums.DelFlagEnum;
import com.plan.service.card.TaskRepositoryService;
import com.plan.service.card.TeamService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 任务库仓库里的任务操作
 *
 * @author zcl
 */
@RestController
@RequestMapping("taskRepository/")
public class TaskRepositoryController {
    @Resource
    private AppProperty appProperty;
    @Resource
    private TeamService teamService;
    @Resource
    private TaskRepositoryService taskRepositoryService;

    /**
     * 查询积分卡对应年龄水平的系统推荐任务
     * 参数的teamId和cardId是为了查回当前积分卡的年龄，然后返回对应年龄水平的推荐任务
     */
    @PostMapping(value = "sysList")
    public Result<List<TaskListVO>> sysList(@RequestBody BaseReq req, HttpServletRequest httpRequest) {
        if (req.getTeamId() == null || req.getCardId() == null) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        // 考虑到只是查询，为了性能，选择不校验权限。
        return taskRepositoryService.sysListFromCache(req);
    }

    @PostMapping(value = "myList")
    public Result<List<TaskListVO>> myList(@RequestBody BaseReq req, HttpServletRequest httpRequest) {
        if (req.getTeamId() == null || req.getCardId() == null) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        // 考虑到只是查询，为了性能，选择不校验权限
        return taskRepositoryService.myList(req);
    }

    /**
     * 创建自定义任务库里的任务
     */
    @PostMapping(value = "add")
    public Result<Long> add(@RequestBody MyTask task, HttpServletRequest httpRequest) {
        UserVO vo = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        if (task.getName() == null || task.getPoint() == null || task.getTeamId() == null) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        UserVO user = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        if (!teamService.isUserInTeam(task.getTeamId(), user.getId())) {
            return Result.fail(BaseCode.ILLEGAL_OPERATION);
        }
        task.setCreatorId(vo.getId());
        task.setUpdaterId(vo.getId());
        task.setDelFlag(DelFlagEnum.NORMAL.getType());
        Result<Long> r = taskRepositoryService.insert(task);
        taskRepositoryService.clearMyTaskCache(task.getTeamId(), task.getCardId());
        return r;
    }

    /**
     * 追加任务
     */
    @PostMapping(value = "addAfter")
    public Result<Long> addAfter(@RequestBody MyTask task, HttpServletRequest httpRequest) {
        UserVO vo = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        if (task.getName() == null || task.getPoint() == null || task.getTeamId() == null) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        UserVO user = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        if (!teamService.isUserInTeam(task.getTeamId(), user.getId())) {
            return Result.fail(BaseCode.ILLEGAL_OPERATION);
        }

        Result<Long> r = taskRepositoryService.addAfter(task, user);
        taskRepositoryService.clearMyTaskCache(task.getTeamId(), task.getCardId());
        return r;
    }


    /**
     * 删除自定义任务库里的任务
     */
    @PostMapping(value = "delete")
    public Result<Long> delete(Long id, HttpServletRequest httpRequest) {
        if (id == null) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        UserVO user = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        return taskRepositoryService.delete(id, user);
    }

    /**
     * 复制仓库任务
     */
    @PostMapping(value = "copyRepositoryTask")
    public Result<Long> copyRepositoryTask(@RequestBody CopyRepositoryTaskReq req, HttpServletRequest httpRequest) {
        if (req.getTeamId() == null || CollectionUtils.isEmpty(req.getTaskIdList())) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        if (req.getTaskIdList().size() > appProperty.getDayPlanMax()) {
            return Result.fail(BaseCode.DAY_PLAN_LIMIT_ERROR);
        }
        UserVO user = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        if (!teamService.isUserInTeam(req.getTeamId(), user.getId())) {
            return Result.fail(BaseCode.ILLEGAL_OPERATION);
        }
        return taskRepositoryService.copyRepositoryTask(req, user);
    }

}
